package hrms.controller;

import com.alibaba.fastjson.JSONObject;
import hrms.entity.Result;
import hrms.entity.Salary;
import hrms.entity.SalaryConfigInfo;
import hrms.service.SalaryConfigInfoService;
import hrms.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    @Autowired
    private SalaryConfigInfoService salaryConfigInfoService;

    @GetMapping("/configinfo")
    public Result getSalarConfigInfo(){
        SalaryConfigInfo configInfo = salaryConfigInfoService.selectInfo();
        Map<String,Object> map = new HashMap<>();
        map.put("configInfo",configInfo);
        return Result.success().addMap(map);
    }

    @PostMapping("/updateinfo")
    public Result editSalaryConfigInfo(@RequestBody SalaryConfigInfo newConfigInfo){
        System.out.println(newConfigInfo);
        salaryConfigInfoService.updateInfo(newConfigInfo);
        return Result.success();
    }

    @PostMapping("/insert")
    public Result findSalary(@RequestBody Salary salary){
        // 缴费基数
        salary.setCreateTime(new Date());
        List<Salary> list = salaryService.findByDateOrStaffId(salary.getCreateTime().getYear(), salary.getCreateTime().getMonth(), salary.getStaff().getId());
        if (list.size()!=0){
            return Result.fail().error("该员工当月工资已发");
        }else{
            Salary calSalary = salaryService.calSalary(salary);
            Map<String,Object> map = new HashMap<>();
            map.put("salary",calSalary);
            return Result.success().addMap(map);
        }
    }

    @GetMapping("/findByDateOrStaffId")
    public Result findByDate(Integer year,Integer month,Integer staffId){
        System.out.println(year);
        System.out.println(month);
        System.out.println(staffId);
        List<Salary> salarys = salaryService.findByDateOrStaffId(year, month,staffId);
        if(salarys.size() == 0){
            return Result.fail().error("查询不到对应工资，请输入正确信息");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("salarys",salarys);
        return Result.success().addMap(map);
    }

}
